package mest.service.LGF.WarehouseWorkFinish.Impl;

import mest.VO.LGF.WarehousePurchase.warehousesave_manytomanyVO;
import mest.VO.LGF.WarehousePurchase.warehousesave_manytomany_recordVO;
import mest.VO.LGF.WarehouseWorkFinish.warehouse_productjob_workfinish_recordVO;
import mest.dao.LGF.WarehouseWorkFinish.IWarehouseWorkFinishDao;
import mest.entity.warehouse_productjob_workfinish_record;
import mest.entity.warehousesave_manytomany;
import mest.service.LGF.WarehouseWorkFinish.IWarehouseWorkFinsihService;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-20 22:16
 */
@Service
public class WarehouseWorkFinsihServiceImpl implements IWarehouseWorkFinsihService {

    @Resource
    HttpServletRequest request;
    //    String theirDb = mestUtil.getCompanyDB(request);
    @Resource
    private IWarehouseWorkFinishDao iwwd;

    @Override
    public Map baseSelectNofinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwwd.countNOFinish(theirDb);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwwd.selectAllNOFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectFinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwwd.countFinish(theirDb);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwwd.selectAllFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map selectWorkFinishDetail(int wwId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwwd.selectWorkFinishDetailCount(theirDb,wwId);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwwd.selectWorkFinishDetail(theirDb,wwId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map selectWorkFinishPastRecord(int wpwId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwwd.selectWorkFinishPastRecordCount(theirDb,wpwId);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwwd.selectWorkFinishPastRecord(theirDb,wpwId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int addRecord(warehouse_productjob_workfinish_recordVO wpwr) {
        String theirDb = mestUtil.getCompanyDB(request);
        wpwr.setTheirDb(theirDb);
        int i = -2;
        int i1 = -2;
        int i2 = -2;
        int i3 = -2;
        int i4 = -2;
        int i31 = -2;
        int i41= -2;
        int i5 = -2;
        int flag = 1;
        System.out.println(wpwr);
        try {
            i = iwwd.addRecord(wpwr);
         i1 = iwwd.updatehasBeenNumber(wpwr);

            //更新warehousesave_manytomany   和 warehousesave_manytomany_record表
            warehousesave_manytomany wsmtm = iwwd.findWSMTMbysfIdAndWfId(theirDb,wpwr.getWfId2(),wpwr.getSfId());

            if(wsmtm == null){
                //数据库wsmtm没联系，包装联系类
                warehousesave_manytomanyVO wsmtm1 = new warehousesave_manytomanyVO();
                wsmtm1.setWfId(wpwr.getWfId2());
                wsmtm1.setSfId(wpwr.getSfId());
                wsmtm1.setNumber(wpwr.getThisNumber());
                wsmtm1.setTheirDb(theirDb);

                //根据联系类插入
                //先插入warehousesave_manytomany表
                i2 = iwwd.addWSMTM( wsmtm1);

                warehousesave_manytomany_recordVO wsmtmrVO = new warehousesave_manytomany_recordVO();
                wsmtmrVO.setWsmId(wsmtm1.getId());
                wsmtmrVO.setStatus("入库");
                wsmtmrVO.setNumber(wpwr.getThisNumber());
                wsmtmrVO.setEfId(wpwr.getEfId());
                wsmtmrVO.setTheirDb(theirDb);

                //再插入warehousesave_manytomany_record 记录表
                i3 = iwwd.addWSMTMRecord( wsmtmrVO);
                i31 = iwwd.updateWSMTMNumber(wsmtmrVO);
            }else{
                flag = 2;
                warehousesave_manytomany_recordVO wsmtmrVO = new warehousesave_manytomany_recordVO();
                wsmtmrVO.setWsmId(wsmtm.getId());
                wsmtmrVO.setStatus("入库");
                wsmtmrVO.setNumber(wpwr.getThisNumber());
                wsmtmrVO.setEfId(wpwr.getEfId());
                wsmtmrVO.setTheirDb(theirDb);
                i4 = iwwd.addWSMTMRecord( wsmtmrVO);
                i41 = iwwd.updateWSMTMNumber(wsmtmrVO);
            }

             i5 = iwwd.updateProductOrderTotalProductNumber(wpwr);

        }catch (Exception e) {
            e.printStackTrace();
        }finally{

            if(flag == 1){
                if(i == -2 || i1 == -2 || i2 == -2 || i3 == -2 || i31 == -2)
                    return -2;
            }else{
                if(i == -2 || i1 == -2 || i4 == -2 || i41 == -2)
                    return -2;
            }
            if(i5 == -2)
                return -2;

            if(i == -1 || i1 == -1 || i2 ==-1 || i3 == -1 || i31 == -1 || i4 == -1 || i41 == -1 || i5 == -1)
                return -1;
            }
        return i;
    }

    @Override
    public int updateByWWId(int wwId, int finishStatus) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i =-2;
        try {
            i = iwwd.updateByWWId(theirDb,wwId,finishStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}
